package android.slc.corelifecycle.utils;

import android.app.Activity;

import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;

import com.blankj.utilcode.util.ActivityUtils;

/**
 * @author android.slc
 * @date 2021/4/2 12:43
 */
public class RxLifecycleUtils {
    public static RxLifecycleDelegate createByTopActivity() {
        Activity activity = ActivityUtils.getTopActivity();
        if (ActivityUtils.getTopActivity() instanceof LifecycleOwner) {
            LifecycleOwner lifecycleOwner = (LifecycleOwner) activity;
            return RxLifecycleDelegateImp.create(lifecycleOwner.getLifecycle());
        }
        return null;
    }

    public static SlcLifecycleProvider createPlaceholderDelegate() {
        return RxManualDelegate.create();
    }

    public static void manualSendEvent(SlcLifecycleProvider slcLifecycleProvider, Lifecycle.Event event) {
        if (slcLifecycleProvider instanceof RxManualDelegate) {
            RxManualDelegate rxManualDelegate = (RxManualDelegate) slcLifecycleProvider;
            rxManualDelegate.sendEvent(event);
        }

    }

    public static void manualSendDestroy(SlcLifecycleProvider slcLifecycleProvider) {
        manualSendEvent(slcLifecycleProvider, Lifecycle.Event.ON_DESTROY);
    }
}
